<?php
namespace Admin\Frontend\Controllers;

use Helpers\Main;
use Helpers\Assets;
use Helpers\Template;
use Admin\Frontend\Models\Admire;
use Helpers\UserPower;
use Admin\Models\sys\XhsysConfig;

class AdmireController extends ControllerBase
{
    /**
     * 通过配置获取接口host地址
     *
     * @return int
     *
     * create by hxc 20171205
     * modify by ck 20171228
     */
    private function getHost(){
        return Main::getConfigByName('admire')['url'];
//        $config = XhsysConfig::findFirst("sConfigName = 'admire' AND bDelete = 0");
//        if(!$config) {
//            return 0;
//        }
//        $configData = json_decode($config->mConfigValue);
//        $host = $configData->host;
//        return $host;
    }

    /**
     * 学海美文列表页
     *
     * @return mixed
     *
     * create by hxc 20171204
     */
    public function articleAction()
    {
        $this->view->pick('admire/article');
        Main::showNavPosition();
        Assets::easyUI('js/admire/article.js');

//        if(!Template::exists('Admire', 'list')){
//            return $this->sendErrorResponse(400,'配置文件不存在，请先创建！');
//        }

        if (!UserPower::checkUserPower('Admire_Article')) {
            return $this->sendViewError('无权限');
        }
        $listSearchArr = $listButtonArr = [];

        $listButtonArr[] = ['id' => 'recover', 'text' => '恢复', 'plain' => 'true', 'icon' => 'fa-reply'];
        $listButtonArr[] = ['id' => 'deny', 'text' => '屏蔽', 'plain' => 'true', 'icon' => 'fa-minus-circle'];
        $listButtonArr[] = ['id' => 'del', 'text' => '删除', 'plain' => 'true', 'icon' => 'fa-trash-o'];

        $listSearchArr[] = ['type' => 'text', 'id' => 'search', 'label' => ' 文章关键词: '];
        return $this->view->setVars([
            'List' => Template::createList('List', 'Admire_Article', [
                'searchToolbar' => Template::createSearchToolBar($listSearchArr, true),
                'buttonToolbar' => Template::createButtonToolBar($listButtonArr)
            ]),
        ]);
    }

    /**
     * 获取学海美文列表数据
     *
     * @return mixed
     *
     * create by hxc 20171204
     */
    public function getAdmireListAction()
    {
        $requestData = $this->request->getPost();
        if($this->request->isPost()) {
            $admireModel = new Admire();
            if(!$host = self::getHost()){
                return $this->sendErrorResponse(400,'接口地址配置错误');
            }
            $admireModel->setHost(self::getHost());
            $data = $admireModel->search($requestData);
            return $this->sendJsonResponse($data);
        }
        else{
            return $this->sendErrorResponse(400,'请求规则错误');
        }
    }

    /**
     * 学海美文操作(屏蔽/恢复/删除)
     *
     * @return mixed
     *
     * create by hxc 20171204
     */
    public function admireCtrlAction(){
        $this->view->disable();
        if($this->request->isPost()){
            $requestData = $this->request->getPost();
            $admireModel = new Admire();
            if(!$host = self::getHost()){
                return $this->sendErrorResponse(400,'接口地址配置错误');
            }
            $admireModel->setHost(self::getHost());
            $data = $admireModel->articleCtrl($requestData);
            return $this->sendJsonResponse($data);
        }
        else{
            return $this->sendErrorResponse(400, '请求规则错误');
        }
    }

    /**
     * 学海美文评论列表页
     *
     * @return mixed
     *
     * create by hxc 20171204
     */
    public function commentAction()
    {
        $this->view->pick('admire/comment');
        Main::showNavPosition();
        Assets::easyUI('js/admire/comment.js');

        if(!Template::exists('Admire_Comment', 'list')){
            return $this->sendErrorResponse(400,'配置文件不存在，请先创建！');
        }
        $listSearchArr = $listButtonArr = [];

        $listButtonArr[] = array('id' => 'recover', 'text' => '恢复', 'plain' => 'true', 'icon' => 'fa-reply');
        $listButtonArr[] = array('id' => 'deny', 'text' => '屏蔽', 'plain' => 'true', 'icon' => 'fa-minus-circle');
        $listButtonArr[] = array('id' => 'del', 'text' => '删除', 'plain' => 'true', 'icon' => 'fa-trash-o');

        $listSearchArr[] = ['type'=>'text', 'id'=>'search', 'label'=> '评论关键词: '];
        return $this->view->setVars([
            'List' => Template::createList('List', 'Admire_Comment', [
                'searchToolbar' => Template::createSearchToolBar($listSearchArr, true),
                'buttonToolbar' => Template::createButtonToolBar($listButtonArr)
            ]),
        ]);
    }

    /**
     * 获取评论列表
     *
     * @return mixed
     *
     * create by hxc 20171204
     */
    public function getAdmireCommentListAction()
    {
        $requestData = $this->request->getPost();
        if($this->request->isPost()){
            $admireModel = new Admire();
            if(!$host = self::getHost()){
                return $this->sendErrorResponse(400,'接口地址配置错误');
            }
            $admireModel->setHost(self::getHost());
            $data = $admireModel->searchComment($requestData);
            return $this->sendJsonResponse($data);
        }
        else{
            return $this->sendErrorResponse(400,'请求规则错误');
        }
    }

    /**
     * 评论操作(屏蔽/恢复/删除)
     *
     * @return mixed
     *
     * create by hxc 20171204
     */
    public function admireCommentCtrlAction(){
        if($this->request->isPost()){
            $requestData = $this->request->getPost();
            $admireModel = new Admire();
            if(!$host = self::getHost()){
                return $this->sendErrorResponse(400,'接口地址配置错误');
            }
            $admireModel->setHost(self::getHost());
            $data = $admireModel->commentCtrl($requestData);
            return $this->sendJsonResponse($data);
        }
        else{
            return $this->sendErrorResponse(400,'请求规则错误');
        }
    }


}
